package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTxnNumConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.facade.bean.BizInfBean;
import cc.rengu.igas.channel.nucc.facade.bean.PyeeInfBean;
import cc.rengu.igas.channel.nucc.facade.bean.PyerInfBean;
import cc.rengu.igas.channel.nucc.facade.request.RefundPaymentAcqRequest;
import cc.rengu.igas.channel.nucc.facade.request.RefundPaymentRequest;
import cc.rengu.igas.channel.nucc.facade.response.CommonTransAcqResponse;
import cc.rengu.igas.channel.nucc.facade.response.CommonTransResponse;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.FinanceTxnMapper;
import cc.rengu.oltp.service.common.dao.impl.FinanceTxnMapperImpl;
import cc.rengu.oltp.service.common.entity.FinanceTxn;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.TimeUtil;
import com.alibaba.fastjson.JSON;

/**
 * 退款服务
 */
public class EpccRefundPaymentService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        RefundPaymentRequest request = new RefundPaymentRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.MCHNT_ORDER_ID, request.getOrderId());
        String txnAmt = request.getTrxAmt();
        String transCurrency = txnAmt.substring(0, 3);
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.TRANS_CCY, transCurrency);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TRANS_AMT, txnAmt);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_CCY, request.getTrxAmt().substring(0, 3));
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_AMT, request.getTrxAmt().substring(3));
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ISS_INST_CODE, request.getPyerAcctIssrId());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.PAY_ACCT_TYPE, request.getPyerAcctTp());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.PAY_ACCT_INFO, request.getPyerAcctId());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        RefundPaymentRequest innerRequest = (RefundPaymentRequest) request;
        String sysSeqNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        String oriTrxId = innerRequest.getOriTrxId();
        String oriTrxDtTm = innerRequest.getOriTrxDtTm();
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        //查询原支付交易
        FinanceTxnMapper txnMapper = new FinanceTxnMapperImpl();
        FinanceTxn orgFinanceTxn = txnMapper.selectFinanceTxnInfoByFrontSeqNum(innerRequest.getHeader().getInstId(), oriTrxDtTm, oriTrxId, NuccParamConstant.TUNL_ID_NUCC, innerRequest.getHeader().getChanlId());
        if (null == orgFinanceTxn) {
            rglog.error("根据前端流水号[{}]查询原交易失败!", oriTrxId);
            throw new BizException(RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        xmlTreeUtil.setXmlTreeObjectValue(NuccTreeNodeConstant.ORG_TXN_INFO, orgFinanceTxn);

        NuccService nuccService = new NuccServiceImpl();
        RefundPaymentAcqRequest acqRequest = initRefundPaymentAcqRequest(xmlTreeUtil, innerRequest);

        CommonTransAcqResponse acqResponse = nuccService.refundPayment(acqRequest);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());
            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            BizInfBean bizInf = new BizInfBean();
            bizInf.setDbtrBankId(acqResponse.getDbtrBankId());
            bizInf.setCdtrBankId(acqResponse.getDbtrBankId());
            bizInf.setTrxStatus(acqResponse.getTrxStatus());
            bizInf.setTrxFinishTm(acqResponse.getTrxFinishTm());
            bizInf.setBatchId(acqResponse.getBatchId());
            bizInf.setSgnNo(acqResponse.getSgnNo());

            CommonTransResponse response = new CommonTransResponse();
            response.setBizInf(bizInf);

            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
            bizResponse.setResult(response);
        }
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        CommonTransResponse commonTransResponse = (CommonTransResponse) response.getResult();
        ConvertUtil.convertInput(commonTransResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    private RefundPaymentAcqRequest initRefundPaymentAcqRequest(XmlTreeUtil xmlTreeUtil, RefundPaymentRequest innerRequest) throws Exception {
        RefundPaymentAcqRequest refundPaymentAcqRequest = new RefundPaymentAcqRequest();
        PyerInfBean pyerInf = new PyerInfBean();
        pyerInf.setPyerAcctIssrId(innerRequest.getPyerAcctIssrId());
        pyerInf.setPyerAcctTp(innerRequest.getPyerAcctTp());
        pyerInf.setPyerAcctId(innerRequest.getPyerAcctId());
        refundPaymentAcqRequest.setPyerInf(pyerInf);

        PyeeInfBean pyeeInf = new PyeeInfBean();
        pyeeInf.setPyeeAcctIssrId(innerRequest.getPyeeAcctTp());
        pyeeInf.setPyeeAcctTp(innerRequest.getPyeeAcctTp());
        pyeeInf.setSgnNo(innerRequest.getSignNo());
        refundPaymentAcqRequest.setPyeeInf(pyeeInf);

        refundPaymentAcqRequest.setResfdAcctIssrId(innerRequest.getResfdAcctIssrId());
        refundPaymentAcqRequest.setInstgAcctId(innerRequest.getInstgAcctId());
        refundPaymentAcqRequest.setInstgAcctNm(innerRequest.getInstgAcctNm());
        refundPaymentAcqRequest.setTrxCtgy(innerRequest.getTrxCtgy());

        /*获取网联流水号*/
        NuccSeqNoManage NuccSeqNoManage = new NuccSeqNoManageImpl();
        String epccTxnId = NuccSeqNoManage.genEpccSeqNo();
        refundPaymentAcqRequest.setTrxId(epccTxnId);
        /*金融交易流水表没有通道流水字段，用remark1来保存*/
        //todo for test
        epccTxnId = "202004200420949494994949484";
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK1, epccTxnId);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM, epccTxnId);
        refundPaymentAcqRequest.setTrxDtTm(TimeUtil.getDateTime("yyyy-MM-dd'T'HH:mm:ss"));
        refundPaymentAcqRequest.setTrxAmt(innerRequest.getTrxAmt());

        FinanceTxn orgFinanceTxn = (FinanceTxn) xmlTreeUtil.getXmlTreeObjectValue(NuccTreeNodeConstant.ORG_TXN_INFO, FinanceTxn.class);

        refundPaymentAcqRequest.setOriTrxId(orgFinanceTxn.getRemark1());
        refundPaymentAcqRequest.setOriBankId(innerRequest.getOriDbtrBankId());
        refundPaymentAcqRequest.setOriTrxAmt(orgFinanceTxn.getOrderCcy() + orgFinanceTxn.getOrderAmt());

        return refundPaymentAcqRequest;
    }
}
